package com.example.nebula.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.nebula.dto.graph.GraphBatchCreateVertex;
import com.example.nebula.dto.graph.GraphCreateVertex;
import com.example.nebula.dto.graph.GraphDeleteVertex;
import com.example.nebula.service.GraphCommonService;
import com.example.nebula.util.NebulaUtil;
import com.example.nebula.util.R;
import com.example.nebula.util.ThreadPoolService;
import com.example.nebula.vo.CommonVo;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @author fulin
 * 点控制器
 */
@RestController
@Api(tags = "点编辑(实体)控制器")
@RequestMapping("/vertex")
@Slf4j
public class VertexManageController {

    @Autowired
    GraphCommonService graphCommonService;

    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(ThreadPoolService.listeningExecutor);


    @PostMapping("/createVertex")
    @ApiOperation("创建点(需要附加标签tag信息)")
    public R<List<CommonVo>> createVertex(@RequestBody GraphCreateVertex graphCreateVertex) {
        String vidType = graphCommonService.getVidType(graphCreateVertex.getSpace());
        return R.data(graphCommonService.executeJson(NebulaUtil.createPoint(graphCreateVertex, vidType), CommonVo.class));
    }

    @PostMapping("/deleteVertex")
    @ApiOperation("删除点(根据点id删除)")
    public R<List<CommonVo>> deleteVertex(@RequestBody GraphDeleteVertex graphDeleteVertex) {
        String vidType = graphCommonService.getVidType(graphDeleteVertex.getSpace());
        return R.data(graphCommonService.executeJson(NebulaUtil.deleteVertex(graphDeleteVertex, vidType), CommonVo.class));
    }

    @PostMapping("/createVertexs")
    @ApiOperation("批量创建点(需要附加标签tag信息)")
    public R<List<CommonVo>> createVertexs(@RequestBody GraphBatchCreateVertex graphCreateVertexs) {
        List<ListenableFuture<CommonVo>> futures = Lists.newArrayList();//监听现场和获取返回值用
        String vidType = graphCommonService.getVidType(graphCreateVertexs.getSpace());
        for (GraphCreateVertex graphCreateVertex : graphCreateVertexs.getVertexs()) {
            graphCreateVertex.setSpace(graphCreateVertexs.getSpace());
            Callable<CommonVo> task = new Callable<CommonVo>() {
                @Override
                public CommonVo call() throws Exception {
                    List<CommonVo> result = graphCommonService.executeJson(NebulaUtil.createPoint(graphCreateVertex, vidType), CommonVo.class);
                    CommonVo vo = result.get(0);
                    vo.setVid(  graphCreateVertex.getPointKey());
                    return vo;
                }
            };
            futures.add(executorService.submit(task));
        }
        ListenableFuture<List<CommonVo>> resultsFuture = Futures.successfulAsList(futures);
        List<CommonVo> results = null;
        try {
            results = resultsFuture.get(5, TimeUnit.MINUTES);
        } catch (Exception e) {
            log.error("createVertexs  error, msg [{}]", e.getMessage());
        }
        return R.data(results);
    }
}
